package com.keyware.utils;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.Page;


/**
 * 
 * 此类描述的是： 多连接分页类  
 * @author: 李军荣   
 * @version: 2017年12月8日 上午9:57:23
 */
public class PageUtils{

	/**
	 * 
	 * @Title: getPageBefore
	 * @Description: 拼出来的list进行分页(常规分页方式)
	 * @param paginationUtil
	 * @param testRequirementList
	 * @author 李军荣
	 * @return void
	 */
    public void getPageBefore(PaginationUtil paginationUtil,List<?> list,HttpServletRequest request){
    	setRowDataPageBefore(paginationUtil, list, null, request);
    } 
    
    /**
     * 
     * @Title: getRowPageData
     * @Description: 获取界面需要展示的数据
     * @param list
     * @param request
     * @return
     * @author 赵亚舟
     * @return List<?>
     */
    public List<?> getRowPageData(List<?> list,HttpServletRequest request){
    	int pageSize =  Integer.parseInt(request.getParameter("pageSize")== null ? "1" : request.getParameter("pageSize"));
		int pageNo = Integer.parseInt(request.getParameter("pageNo") == null ? "10" : request.getParameter("pageNo"));
    	int starNum = 0;
		if(pageNo == 1){
			starNum = 0;
		}else{
			starNum = (pageNo-1)*pageSize;
		}
		int endNum = pageNo*pageSize;
		Page<Object> pageData = getPageData(list, starNum, endNum);
	    return pageData;
    }
    
    /**
     * 
     * @Title: getRowPageData
     * @Description: 往前台返回数据(项目文档列表展示分页)
     * @param paginationUtil
     * @param list
     * @param rowlist
     * @param request
     * @author 赵亚舟
     * @return void
     */
    public void setRowPageData(PaginationUtil paginationUtil,List<?> list,List<?> rowlist,HttpServletRequest request){
    	setRowDataPageBefore(paginationUtil, list, rowlist, request);
    }
    
    
    
   
    private void setRowDataPageBefore(PaginationUtil paginationUtil,List<?> list,List<?> rowlist,HttpServletRequest request){
    	int pageSize =  Integer.parseInt(request.getParameter("pageSize") == null ? "1" : request.getParameter("pageSize"));
		int pageNo = Integer.parseInt(request.getParameter("pageNo") == null ? "10" : request.getParameter("pageNo"));
		int starNum = 0;
		if(pageNo == 1){
			starNum = 0;
		}else{
			starNum = (pageNo-1)*pageSize;
		}
		int endNum = pageNo*pageSize;
		
	    
	    if(list.size()>0){
	    	if(list.size() <= starNum){
	    		pageNo = 1;
	    	}
	    }
	    Page<Object> p = new Page<Object>();
	    if(rowlist == null){
	    	p = getPageData(list, starNum, endNum);
	    }else{
	    	for(int i=0;i<rowlist.size();i++){
	    		p.add(rowlist.get(i));
	    	}
	    }
	    
    	p.setPageSize(pageSize);
	    p.setTotal(list.size());
	    paginationUtil.after(p);
		paginationUtil.setPageNo(pageNo);
    }

	private Page<Object> getPageData(List<?> list, int starNum, int endNum) {
		int i = 0;
	    Page<Object> p = new Page<Object>();
	    if(list.size()>0){
	    	if(list.size() > starNum){
				for(Object object : list){
					i++;
					if(i>starNum && i<=endNum){
						p.add(object);
					}	
				}
	    	}else{
	    		for(Object object : list){
					i++;
					if(i>0 && i<=10){
						p.add(object);
					}	
				}
	    	}	
	    }
		return p;
	}
}
